package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.meterware.httpunit.Button;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

/**
 * Demonstrates that the ResearchersActionBean is working.
 * 
 * @author Edward Kim
 * @author JianFei Liao
 * @author KaYee Leung
 */
public class TestResearchersActionBean {
  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  
  /**
   * Tests to view all researchers.
   * 
   * @throws Exception If error occur.
   */
  @Test
  public void testViewAllResearchers() throws Exception {
    WebConversation conversation = new WebConversation();
    
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern-gold";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Checking index.jsp retrieval", "MyISERN 1.3 - Home", response.getTitle());
    
    // login the system
    WebForm loginForm = response.getFormWithID("loginForm");
    loginForm.setParameter("username", "isern");
    loginForm.setParameter("password", "password");
    response = loginForm.submit();
    
    // check if the user is login successfully by clicking the "View All Researchers" link again
    WebLink link = response.getLinkWith("View All Researchers");
    response = link.click();
    // the page should become researchers.jsp
    assertEquals("Clicking link after login", 
        "MyISERN 1.3 - View All Researchers", response.getTitle());
    
    // check the researchers table
    WebTable researchersTable = response.getTableWithID("researchersTable");
    // check the first researcher in the table
    String name = researchersTable.getCellAsText(0, 0);
    assertEquals("Checking researchers table", "Philip Johnson", name);
    
    // clicks on the first researcher to edit
    link = response.getLinkWith(name);
    response = link.click();
    // the page should become editResearcher.jsp
    assertEquals("Clicking a researcher to edit", 
        "MyISERN 1.3 - Edit Researcher", response.getTitle());
    
    // checks if the current researcher being edited is correct
    WebForm editForm = response.getFormWithID("editSingleResearcherForm");
    name = editForm.getParameterValue("researcher.name");
    assertEquals("Checking editing researcher name", "Philip Johnson", name);
  
    // changes the organization attribute and save it
    editForm.setParameter("researcher.organization", "University of Hawaii");
    Button button = editForm.getButtonWithID("saveButton");
    button.click();
    response = conversation.getCurrentPage();
    // the page should now go back to researchers.jsp
    assertEquals("Checking save after changing", 
        "MyISERN 1.3 - View All Researchers", response.getTitle());
    
    // tries to add a new researcher, but without any data
    link = response.getLinkWith("Add New Researcher");
    response = link.click();
    // the page should become addResearcher.jsp
    assertEquals("Click to add a new researcher", 
        "MyISERN 1.3 - Add Researcher", response.getTitle());
    // leave everything blank by setting them to null
    WebForm addForm = response.getFormWithID("addSingleResearcherForm");
    button = addForm.getButtonWithID("saveButton");
    button.click();
    response = conversation.getCurrentPage();
    // the page should be addResearcher.jsp still, since the name is required
    assertEquals("Checking save after adding without a name", 
        "MyISERN 1.3 - Add Researcher", response.getTitle());
    
    // tries to edit a researcher via the link from sanity report
    link = response.getLinkWith("Sanity Report");
    response = link.click();
    // the page should become report.jsp
    assertEquals("Check the sanity report", 
        "MyISERN 1.3 - Report", response.getTitle());
    // click on the first link on the report table
    WebTable reportTable = response.getTableWithID("reportTable");
    String linkString = reportTable.getCellAsText(1, 0);
    assertEquals("Checking the first invalid link in the report",
        "UHM in researcher: Fei Liao", linkString);
    link = response.getLinkWith(linkString);
    response = link.click();
    // the page should become editResearcher.jsp
    assertEquals("Clicking sanity report link to edit", 
        "MyISERN 1.3 - Edit Researcher", response.getTitle());
    // verifies the editing researcher
    editForm = response.getFormWithID("editSingleResearcherForm");
    name = editForm.getParameterValue("researcher.name");
    assertEquals("Checking the editing researcher", "Fei Liao", name);
  } 
}